using System;
using System.Web.UI.WebControls;
using GURUCORE.Framework.Core.Data;

using GURUCORE.Lib.Core;


namespace GURUCORE.Portal.Core.BasePagelet
{
    public abstract class EditPagelet : Pagelet
    {
        protected const string ACTION_SAVE = "Save";
        protected const string ACTION_CANCEL = "Cancel";
        protected const string ACTION_DELETE = "Delete";

        //[PageletSessionfulMember]      
        /// <summary>
        /// data before bind to control on form. It will be data after OnCollectData (when btnSave click)
        /// </summary>
        protected DTOBase m_dtoData;

        private Button m_btnSave;
        private Button m_btnCancel;
        private Button m_btnDelete;




        [PageletInParam("ThumbnailSize", PageletInParamAttribute.PAGE_CONFIGURATION_PARAM_SOURCE)]
        protected string m_sThumbnailSize;

        [PageletInParam("ThumbnailRelativePath", PageletInParamAttribute.PAGE_CONFIGURATION_PARAM_SOURCE)]
        protected string m_sThumbnailRelativePath;

        [PageletInParam("ThumbnailLocalized", PageletInParamAttribute.PAGE_CONFIGURATION_PARAM_SOURCE)]
        protected bool m_bThumbnailLocalized;

        [PageletInParam("ImageFieldName", PageletInParamAttribute.PAGE_CONFIGURATION_PARAM_SOURCE)]
        protected string m_sImageFieldName;







        protected override void OnPageletInit()
        {
            m_btnSave = (Button) FindControl("btnSave");
            m_btnSave.Click += new EventHandler(m_btnSave_Click);
            m_btnSave.CausesValidation = true;

            m_btnCancel = (Button) FindControl("btnCancel");
            m_btnCancel.Click += new EventHandler(m_btnCancel_Click);
            m_btnCancel.CausesValidation = false;

            m_btnDelete = (Button) FindControl("btnDelete");
            if (m_btnDelete != null)
            {
                m_btnDelete.Click += new EventHandler(m_btnDelete_Click);                
                m_btnDelete.CausesValidation = false;
            }
        }

        private void m_btnCancel_Click(object sender, EventArgs e)
        {
            OnCancel();
            Navigate(ACTION_CANCEL);
        }

        private void m_btnDelete_Click(object sender, EventArgs e)
        {
            OnDelete();
            Navigate(ACTION_DELETE);
        }

        private void m_btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                DTOBase dtoData = AutoCollectData(m_dtoData, this, true); //CollectData();

                //assign ID
                OnCollectData(ref dtoData);
                m_dtoData = dtoData;

                if (SaveData(dtoData) > -1)
                {
                    OnSave();
                    Navigate(ACTION_SAVE);
                }
            }

        }

        protected virtual void OnCollectData(ref DTOBase p_dtoData)
        {
        }

        protected abstract int SaveData(DTOBase p_dtoData);

        protected abstract DTOBase GetData();

        protected override void OnPageletLoad()
        {
            m_dtoData = GetData();
            if (!IsCreateNew())
            {
                AutoBindData(m_dtoData, this, true);
                OnBindData(m_dtoData);
            }
        }

        protected override void OnPageletPostBack()
        {
            m_dtoData = GetData();
        }

        protected virtual void OnBindData(DTOBase p_dtoData)
        {
        }

        protected abstract bool IsCreateNew();

        /// <summary>
        /// Create thumbnail file for Image field if existed
        /// </summary>
        protected virtual void OnSave()
        {
            if (null != m_dtoData && null != m_sThumbnailSize)  // have data, and need to resize image to thumbnails
            {
                if (string.IsNullOrEmpty(m_sImageFieldName))
                {
                    m_sImageFieldName = "Image";
                }

                string[] arrImageFieldName = m_sImageFieldName.Split(',');
                foreach (string sImageFieldName in arrImageFieldName)
                {
                    string sActualImageFileName = ConvertSafe.ToString(GURUCORE.Lib.Core.Reflection.ReflectionHelper.GetPropertyValueOf(m_dtoData, sImageFieldName));

                    if (string.IsNullOrEmpty(sActualImageFileName) == false)    // only process with dto contain image
                    {
                        string[] arrThumbsize = m_sThumbnailSize.Split(',');
                        try // create thumbnail, if can't create, maybe provided data file is NOT IMAGE
                        {
                            this.CreateThumbnailImage(sActualImageFileName, m_sThumbnailRelativePath, m_bThumbnailLocalized, arrThumbsize);
                        }
                        catch (OutOfMemoryException)
                        {
                            // the uploaded file is not image, so we can't create thumbnail for it
                            // ignore all more process
                            return;
                        }
                    }
                }



            }
        }


        protected virtual void OnCancel()
        {
        }

        protected virtual void OnDelete()
        {
        }



    }
}